<template>
  <div class="app-container">
    <el-dialog
      :title="dialogtitle"
      :visible.sync="dialogVisible"
      width="98%"
      top="20px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
    >
      <dynamicEdit :infoid="id" :read-only="readOnly" :timestamp="timestamp" @dialogHide="dialogHide($event)" />
    </el-dialog>

    <div class="filter-container">
      <!-- <el-select v-model="listQuery.classCode" placeholder="请选择车辆类型" clearable style="margin-left: 10px;width: 200px" class="filter-item">
        <el-option label="全部" value="" />
        <el-option v-for="(it,index) in classList" :key="index" :label="it.label" :value="it.value" />
      </el-select> -->
      <el-input v-model="listQuery.rnamenum" placeholder="检验项目" style="width: 200px;margin-left: 10px;" class="filter-item" />
      <el-input v-model="listQuery.rstandard" placeholder="标准要求" style="width: 200px;margin-left: 10px;" class="filter-item" />
      <el-input v-model="listQuery.rresultcode" placeholder="检验结果代号" style="width: 200px;margin-left: 10px;" class="filter-item" />
      <el-input v-model="listQuery.rname" placeholder="检验结果名称" style="width: 200px;margin-left: 10px;" class="filter-item" />
      <el-input v-model="listQuery.rtype" placeholder="结果类型" style="width: 200px;margin-left: 10px;" class="filter-item" />
      <!-- <el-date-picker v-model="listQuery.publishTime" placeholder="请选择日期" style="width: 200px;margin-left: 10px;" value-format="yyyy-MM-dd" class="filter-item" /> -->
      <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilter">
        搜索
      </el-button>

      <!-- <el-button v-if="roleEdit" style="margin-left: 10px;float:right" class="filter-item" type="primary" icon="el-icon-download" @click="showDic">
        类别管理
      </el-button> -->
      <el-button class="filter-item" style="margin-left: 10px;float:right" type="success" icon="el-icon-edit" @click="addnew">
        新建
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;float:right" type="primary" icon="el-icon-upload">
        批量导入
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;float:right" type="" icon="el-icon-download">
        下载模板
      </el-button>
    </div>

    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" prop="r_number" label="序号" width="80" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.rnumber }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="r_namenum" label="检验项目" width="300" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.rnamenum }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="300px" prop="r_standard" label="标准要求" show-overflow-tooltip sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.rstandard }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="r_resultcode" label="检验结果代号" width="150" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.rresultcode }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="r_name" label="检验结果名称" width="150" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.rname }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="r_type" label="结果类型" width="120" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.rtype }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="220">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="edit(scope.row.id)">
            编辑
          </el-button>
          <el-popconfirm title="确定删除该信息吗？" style="margin-left:10px;" @onConfirm="deleteData(scope.row.id)">
            <el-button slot="reference" type="danger" size="small" icon="el-icon-delete">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import dynamicEdit from './edit'

export default {
  name: 'ArticleList',
  components: { Pagination, dynamicEdit },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      roleEdit: false,
      rolePublisher: false,
      selection: false,
      dicListLoading: false,
      stopDicLoading: false,
      startDicLoading: false,
      submitLoading: false,
      classList: [],
      classForm: {
        parentClass: 14,
        label: ''
      },
      diclist: [],
      settingVisible: false,
      item: {},
      dialogtitle: '新增',
      dialogVisible: false,
      readOnly: false,
      timestamp: false,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        rname: '',
        rnamenum: '',
        rresultcode: '',
        rstandard: '',
        rtype: '',
        page: 1,
        limit: 10
      },
      Sortkey: '',
      Isace: '',
      multipleSelection: [],
      id: ''
    }
  },
  created() {
    this.getList()
    // this.getMyRole()
  },
  mounted() {
    if (JSON.parse(localStorage.getItem('user')).auths.includes('AUTH_close')) {
      this.roleEdit = true
    } else {
      this.roleEdit = false
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteData(id) {
      let ids = []
      if (id) {
        ids = [id]
      } else {
        ids = this.multipleSelection.map(item => item.id)
      }
      if (ids.length > 0) {
        this.$http
          .post('/api/resultcols/removeResultCols', { ids: ids })
          .then(response => {
            if (response.data.success === true) {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.getList()
            } else {
              console.log('删除失败:' + response.data.message)
              this.$message({
                message: '删除失败:' + response.data.message,
                type: 'error'
              })
            }
          })
          .catch(error => {
            console.log(error)
          })
      } else {
        this.$message({
          message: '请至少选择一条信息',
          type: 'danger'
        })
      }
    },
    sortChange(column, prop, order) {
      this.Sortkey = column.prop
      if (column.order === 'ascending') {
        this.Isace = 'asc'
      } else {
        this.Isace = 'desc'
      }
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    addnew() {
      this.id = 0
      this.readOnly = false
      this.dialogtitle = '新增'
      this.dialogVisible = true
      this.timestamp = new Date().getTime()
    },
    edit(id) {
      this.id = id
      this.readOnly = false
      this.dialogtitle = '编辑'
      this.dialogVisible = true
      this.timestamp = new Date().getTime()
    },
    detail(id) {
      this.id = id
      this.readOnly = true
      this.dialogtitle = '查看'
      this.dialogVisible = true
      this.timestamp = new Date().getTime()
    },
    dialogHide(flag) {
      this.dialogVisible = false
      this.getList()
    },
    getList() {
      const para = {
        delFlag: 0,
        rname: this.listQuery.rname,
        rnamenum: this.listQuery.rnamenum,
        rresultcode: this.listQuery.rresultcode,
        rstandard: this.listQuery.rstandard,
        rtype: this.listQuery.rtype,
        orderField: this.Sortkey,
        orderType: this.Isace,
        pageNum: this.listQuery.page,
        pageSize: this.listQuery.limit
      }
      this.listLoading = true
      this.$http
        .post('/api/resultcols/queryByPage', para)
        .then(response => {
          this.listLoading = false
          if (response.data.success === true) {
            this.list = response.data.result.records
            this.total = response.data.result.total
            if (response.data.result.current > response.data.result.pages && response.data.result.pages !== 0) {
              this.listQuery.page = response.data.result.pages
              this.getList()
            }
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.listLoading = false
        })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
